class Solution
{
public:
    int longestConsecutive(vector<int>& nums)
    {
        int ans = 0;
        unordered_set<int> num_set(nums.begin(), nums.end());
        int ansLen;
        for (int num : num_set)
        {
            if (!num_set.count(num - 1))
            {
                ansLen = 1;
                while (num_set.count(++num)) ansLen++;
                ans = max(ans, ansLen);
            }
        }
        return ans;
    }
};